package com.hk.service.impl;

import com.hk.dao.ICompanyMessageDao;
import com.hk.dao.impl.CompanyMessageDaoImpl;
import com.hk.model.CompanyMessage;
import com.hk.service.ICompanyMessageService;
import com.hk.utils.JdbcMysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: KangWang
 * Date: 2021/5/18 18:08
 * State：公司简介业务逻辑层
 */
public class CompanyMessageServiceImpl implements ICompanyMessageService {

    ICompanyMessageDao iCompanyMessageDao = new CompanyMessageDaoImpl();

    @Override
    public List<CompanyMessage> findAll(Integer page, Integer limit) {
        ResultSet resultSet = iCompanyMessageDao.findAll(page, limit);
        List<CompanyMessage> companyMessageList = transverter(resultSet);
        //        关闭资源
        JdbcMysql.jdbcClose();
        return companyMessageList;
    }

    @Override
    public int count(Integer page, Integer limit) {
        ResultSet resultSet = iCompanyMessageDao.count(page,limit);
        int count = 0;
        try {
            while (resultSet.next()) {
                count = resultSet.getInt(1);
            }
            return count;
        } catch (SQLException e) {
            e.getMessage();
        }
        //        关闭资源
        JdbcMysql.jdbcClose();
        return count;
    }

    @Override
    public CompanyMessage findById(Integer id) {
        ResultSet resultSet = iCompanyMessageDao.findById(id);
        List<CompanyMessage> companyMessageList = transverter(resultSet);
        //        关闭资源
        JdbcMysql.jdbcClose();
        return companyMessageList.get(0);
    }

    @Override
    public int save(CompanyMessage t) {
        return 0;
    }

    @Override
    public int update(CompanyMessage t) {
        return 0;
    }

    @Override
    public int delete(Integer id) {
        return 0;
    }

    /**
     * 将传递的数据进行转换成集合
     * @param resultSet
     * @return
     */
    public List<CompanyMessage> transverter(ResultSet resultSet ) {
        List<CompanyMessage> companyMessageList = new ArrayList<>();
        try {
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String nameEnglish = resultSet.getString("name_english");
                String phone = resultSet.getString("phone");
                String startTime = resultSet.getString("start_time");
                String endTime = resultSet.getString("end_time");
                String idea = resultSet.getString("idea");
                String ideaEnglish = resultSet.getString("idea_english");
                String profile = resultSet.getString("profile");
                String title = resultSet.getString("title");
                String detail = resultSet.getString("detail");
                int state = resultSet.getInt("state");
                String gmtCreate = resultSet.getString("gmt_create");
                String gmtModified = resultSet.getString("gmt_modified");
//                封装对象
                CompanyMessage companyMessage = new CompanyMessage(id,name,nameEnglish,phone,startTime,endTime,idea,ideaEnglish,profile,title,detail,state,gmtCreate,gmtModified);
                companyMessageList.add(companyMessage);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return companyMessageList;

    }

}
